﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace RestaurantBooker.Controls
{
    public partial class DateAndTimePicker : UserControl, INotifyPropertyChanged
    {
        public DateAndTimePicker()
        {
            InitializeComponent();
            

            LayoutRoot.DataContext = this;
        }
        
        
        public void SetDisplayDates(DateTime from, DateTime to)
        {
            dpDate.DisplayDateStart = from;
            dpDate.DisplayDateEnd = to;
        }

        public void ClearBlackoutDates()
        {
            dpDate.BlackoutDates.Clear();
        }

        public void AddBlackoutDates(DateTime from, DateTime to)
        {
            dpDate.BlackoutDates.Add(new CalendarDateRange(
                from,
                to
            ));
        }

        public void SetBlackoutDates(DateTime from, DateTime to)
        {
            dpDate.BlackoutDates.Clear();
            dpDate.BlackoutDates.Add(new CalendarDateRange(
                from,
                to
            ));
        }

        public void RemoveBlackoutDates(DateTime from, DateTime to)
        {
            dpDate.BlackoutDates.Remove(new CalendarDateRange(
                from,
                to
            ));
        }

        public bool ContainsInBlackoutDates(DateTime date)
        {
            return dpDate.BlackoutDates.Contains(date);
        }

        /// <summary>
        /// The <see cref="IsDatePickerEnabled" /> property's name.
        /// </summary>
        public const string IsDatePickerEnabledPropertyName = "IsDatePickerEnabled";

        private bool _isDatePickerEnabled = true;

        /// <summary>
        /// Gets the IsDatePickerEnabled property.
        /// TODO Update documentation:
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public bool IsDatePickerEnabled
        {
            get
            {
                return _isDatePickerEnabled;
            }

            set
            {
                if (_isDatePickerEnabled == value)
                {
                    return;
                }
                _isDatePickerEnabled = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(IsDatePickerEnabledPropertyName);
            }
        }

        
        /// <summary>
        /// If assign True value - old dates will be disabled 
        /// Can be only setted to True - default it false
        /// </summary>
 
        public bool IsDisabledOldDates
        {
            get { return false; }
            set
            {                
                if (value)
                {
                    dpDate.BlackoutDates.AddDatesInPast();
                    dpDate.BlackoutDates.AddDatesInPast();
                }             
            }
        }

        /// <summary>
        /// The <see cref="DateTimeValue" /> dependency property's name.
        /// </summary>
        public const string DateTimeValuePropertyName = "DateTimeValue";

        /// <summary>
        /// Gets or sets the value of the <see cref="DateTimeValue" />
        /// property. This is a dependency property.
        /// </summary>
        public DateTime DateTimeValue
        {
            get
            {
                return ((DateTime)GetValue(DateTimeValueProperty));
            }
            set
            {
                SetValue(DateTimeValueProperty, value);
            }
        }

        /// <summary>
        /// Identifies the <see cref="DateTimeValue" /> dependency property.
        /// </summary>
        public static readonly DependencyProperty DateTimeValueProperty = DependencyProperty.Register(
            DateTimeValuePropertyName,
            typeof(DateTime),
            typeof(DateAndTimePicker),
            new PropertyMetadata(DateTime.MinValue, DateTimeValueChanged));


        private static void DateTimeValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var dtp = d as DateAndTimePicker;
            if (e.NewValue != null && !dtp._isInternalSetDateTimeValue)
            {
                dtp.DatePart = ((DateTime)(e.NewValue)).Date;
                dtp.TimePart = ((DateTime)(e.NewValue));
            }
        }




        /// <summary>
        /// The <see cref="TimePart" /> property's name.
        /// </summary>
        public const string TimePartPropertyName = "TimePart";

        private DateTime _timePart = new DateTime(0);

        /// <summary>
        /// Gets the TimePart property.
        /// TODO Update documentation:
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public DateTime TimePart
        {
            get
            {
                return _timePart;
            }

            set
            {
                if (_timePart == value)
                {
                    return;
                }        
                _timePart = value;

                ChangeDateTimeValue();

                // Update bindings, no broadcast
                RaisePropertyChanged(TimePartPropertyName);
            }
        }

        private void ChangeDateTimeValue()
        {
            _isInternalSetDateTimeValue = true;
            try
            {
                DateTimeValue = DatePart.Date + TimePart.TimeOfDay;
            }
            finally
            {
                _isInternalSetDateTimeValue = false;
            }
        }


        /// <summary>
        /// The <see cref="DatePart" /> property's name.
        /// </summary>
        public const string DatePartPropertyName = "DatePart";

        private DateTime _datePart = DateTime.Today;

        /// <summary>
        /// Gets the DatePart property.
        /// TODO Update documentation:
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public DateTime DatePart
        {
            get
            {
                return _datePart;
            }

            set
            {
                if (_datePart == value)
                {
                    return;
                }
                
                _datePart = value;

                ChangeDateTimeValue();


                // Update bindings, no broadcast
                RaisePropertyChanged(DatePartPropertyName);
            }
        }





        public event PropertyChangedEventHandler PropertyChanged;
        private bool _isInternalSetDateTimeValue;
        protected void RaisePropertyChanged(string PropertyName)
        {
            var tmp = PropertyChanged;
            if (tmp != null)
                tmp(this, new PropertyChangedEventArgs(PropertyName));
        }

        private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
        {
          
        }

        private void dpDate_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            if (SelectionChanged != null)
                SelectionChanged(sender, e);
        }

        public event SelectionChangedEventHandler SelectionChanged;
    }

      
}
